jQuery(document).on("mobileinit", function() {
	jQuery.mobile.autoInitializePage = false;
	$.mobile.hidePageLoadingMsg();
	$.event.special.swipe.horizontalDistanceThreshold = 100;
	$.event.special.swipe.verticalDistanceThreshold = 40;
});

$(document).ready(function() {

	$(".remote").on("swipeleft", function() {
		var id = $(this).attr("data-fid");
		var action = true;
		sendRemoteCommand(id, action);
	});
	$(".remote").on("swiperight", function() {
		var id = $(this).attr("data-fid");
		var action = false;
		sendRemoteCommand(id, action);
	});

	$('.on').click(function() {
		var id = $(this).parent().parent().attr("data-fid");
		var action = true;
		sendRemoteCommand(id, action);
	});

	$('.off').click(function() {
		var id = $(this).parent().parent().attr("data-fid");
		var action = false;
		sendRemoteCommand(id, action);
	});
	$('.floorSelect').click(function() {
		var id = $(this).attr("data-floorid");
		$.ajax({
			url : "/raspi/controller.jsp",
			type : 'POST',
			data : {
				'selectedFloorId' : id
			},
			cache : false,

			// context: document.body,
			success : function(s, x) {
				var newDoc = document.open("text/html", "replace");
				newDoc.write(s);
				newDoc.close();
				// $("html").fadeOut(200).html(s).fadeIn(200);
			}
		});
	});

});

function sendRemoteCommand(id, action) {
	$.post(
			"controller",
			{
				"action" : action,
				"id" : id,
			},
			function(xhr) {
				$('#infoBox').html(xhr).stop(true, true).fadeIn(500)
						.delay(1000).fadeOut(500, function() {
						});
			}).done(function() {
	}).fail(
			function(xhr, textStatus, errorThrown) {
				$('#errorBox').html(xhr.responseText).stop(true, true).fadeIn(
						500).delay(5000).fadeOut(500, function() {
				});
			}).always(function() {

	});
}